var React = require('react');
var ReactDOM = require('react-dom');
var token = $.cookie('token');
var mytoken = JSON.parse(token);

import {Link, Route, Router, browserHistory, hashHistory} from 'react-router';

import CommodityUpperAndLowerFrame from '../component/commodityUpperAndLowerFrame.jsx'
import ConfirmationOfDelivery from '../component/confirmationOfDelivery.jsx'
import DeliverGoods from '../component/deliverGoods.jsx'
import InNewStocks from '../component/inNewStocks.jsx'
import ModificationOfCommodityInformation from '../component/modificationOfCommodityInformation.jsx'
import NewProductRecommendationManagement from '../component/newProductRecommendationManagement.jsx'
import OperationLogView from '../component/operationLogView.jsx'
import PersonalCenter from '../component/personalCenter.jsx';
import RefundProcessing from '../component/refundProcessing.jsx'
import ReturnGoodsHandling from '../component/returnGoodsHandling.jsx'
import ReturnWarehousing from '../component/returnWarehousing.jsx'
import RoleManagement from '../component/roleManagement.jsx'
import UserManagement from '../component/userManagement.jsx'
import Welcome from '../component/welcome.jsx';
import {connect} from 'react-redux';

var MyRouter = React.createClass({
    getInitialState: function () {
        return {routes: []}
    },
    componentDidMount: function () {
        var _this = this;
        $.ajax({
            type: "GET",
            url: "/api/Employee/GetPermissions",
            headers: {
                Authorization: "Bearer " + mytoken.access_token
            },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                _this.setState({routes: data});

            },
            error: function (msg) {
                alert(msg);
            }
        });
    },
    getRoute: function (routes) {
        return routes.map(route => {
            return (<Route
                key={route.PermissionId}
                path={"/" + route.PermissionRoute}
                component={this.getPermission(route.PermissionRoute)}/>)
        })
    },
    getPermission: function (permission) {
        var _component = Welcome;
        switch (permission) {
            case "ConfirmationOfDelivery":
                _component = ConfirmationOfDelivery
                break;
            case "ReturnWarehousing":
                _component = ReturnWarehousing
                break;
            case "OperationLogView":
                _component = OperationLogView
                break;
            case "InNewStocks":
                _component = InNewStocks
                break;
            case "RoleManagement":
                _component = RoleManagement
                break;
            case "UserManagement":
                _component = UserManagement
                break;
            case "ModificationOfCommodityInformation":
                _component = ModificationOfCommodityInformation
                break;
            case "CommodityUpperAndLowerFrame":
                _component = CommodityUpperAndLowerFrame
                break;
            case "DeliverGoods":
                _component = DeliverGoods
                break;
            case "ReturnGoodsHandling":
                _component = ReturnGoodsHandling
                break;
            case "RefundProcessing":
                _component = RefundProcessing
                break;
            case "NewProductRecommendationManagement":
                _component = NewProductRecommendationManagement
                break;
        }
        return _component;
    },
    setRoutes: function () {
        if (this.state.routes.length > 0) {
            return <div className="content-wrapper">
                <section className="content" id="my-content">
                    <Router history={hashHistory}>
                        <Route path="/">
                            <Route path="/PersonalCenter" component ={PersonalCenter}/>
                            <Route path="/Welcome" component ={Welcome}/>{this.getRoute(this.state.routes)}
                        </Route>
                    </Router>
                </section>
            </div>
        } else {
            return <div className="content-wrapper">
                <section className="content" id="my-content"></section>
            </div>
        }

    },
    render: function () {
        return (this.setRoutes())
    }
});
module.exports = MyRouter;